from studenthoods.apps.shop.models import Shop, Order, HoodieSize, HoodieColour
from django.contrib import admin
from studenthoods.apps.shop.utils import generate_shop_slug_hash, set_order_status

class OrderAdmin(admin.ModelAdmin):
    readonly_fields = ('hash', 'status', 'url_cancel_return', 'url_notify', 'url_return', 'get_admin_url', 'ipn')
    search_fields = ('first_name', 'surname','email', 'shop', 'colour','size', 'created_at')
    list_filter = ('shop__name',)
    list_display  = ('id', 'name','email', 'shop', 'colour','size', 'status', 'created_at')
admin.site.register(Order, OrderAdmin)

class HoodieSizeInline(admin.StackedInline):
    model =  HoodieSize
    extra = 0

class HoodieColourAdmin(admin.ModelAdmin):
    model =  HoodieColour
    extra = 0
    inlines = [HoodieSizeInline]
    prepopulated_fields = {'slug': ('colour', )}
    list_filter = ('shop__name',)
    list_display  = ('id', 'colour', 'shop', 'available_sizes')
admin.site.register(HoodieColour, HoodieColourAdmin)

class ShopAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('name', )}
    readonly_fields = ('slug_hash',)
    search_fields = ('id', 'name', 'description', 'price')
    list_display  = ('id', 'name', 'opening_date','closing_date', 'price', 'available_colours','address_required')

    def save_model(self, request, obj, form, change):
        obj.slug_hash = generate_shop_slug_hash(obj.slug)
        obj.save()
admin.site.register(Shop, ShopAdmin)
